package com.ziyi.tiantianshuiyin.util;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class AppConfig {
    private static AppConfig mInstance;
    private String mConfigPath;

    //是否同意隐私政策用户协议
    public final static String IS_CONFIRM_USER_AGREEMENT="is_confirm_user_agreement";
    //微信昵称
    public final static String WXNAME = "wxName";
    //微信头像
    public final static String WXHEAD = "wxHead";
    //登录
    public final static String USER_NAME="user_name";
    //手机号
    public final static String PHONE_NUMBER="phone_number";
    //登录成功
    public final static int LOGIN_RIGHT=2;

    //友盟key
    public final static String UMENG_KEY = "601e555e425ec25f10eed714";

    // 微信id
    public static final String WX_APP_ID = "wx434ebbee823bfaed";
    // 微信secret
    public static final String WX_APP_SECRET = "e776e93b0fc8e9abc95a95188d75d5e4";

    public final static String WECHAT_AVATAR_IS_INSTALL = "wechat_acctar_is_install";
    public final static String VIP_DATE = "vip_date";
    public final static String VIP_IS_OUT = "vip_is_out";


    private AppConfig(Context context) {
        mConfigPath = getAppConfigFilePath(context, "App_Config");

        File configFile = new File(mConfigPath);

        if (!configFile.exists()) {
            configFile.mkdirs();
        }

        File newConfigFile = new File(mConfigPath + File.separator + "config");
        if (!newConfigFile.exists())
            try {
                newConfigFile.createNewFile();
                mConfigPath = newConfigFile.getPath();
                initProperties();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        mConfigPath = newConfigFile.getPath();
    }

    public static AppConfig getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new AppConfig(context);
        }

        return mInstance;
    }

    public static String getAppConfigFilePath(Context context, String path) {
        String configPath =
                Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ?//getExternalCacheDir(context,path).getPath()
                        getExternalCacheDir(context, path).getPath() :
                        context.getCacheDir().getPath() + "/" + path;

        return configPath;
    }

    public static File getExternalCacheDir(Context context, String path) {
        final String cacheDir = "/Android/data/" + context.getPackageName() + "/" + path + "/";

        if (isEmpty(Environment.getExternalStorageDirectory().getPath()))
            return new File("/mnt/sdcard" + cacheDir);
        else {
//            LogUtil.i("context.getFilesDir(): " + context.getFilesDir().getAbsolutePath());
            if (hasExternalSecondaryStorage()) {
                return new File(context.getFilesDir().getPath() + "/" + path);
            }
            return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
        }
    }

    private static boolean hasExternalSecondaryStorage() {
        boolean ret = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            File externalFile = Environment.getExternalStorageDirectory();
//            LogUtil.i("hasExternalSecondaryStorage: " + externalFile.getAbsolutePath());
            if (externalFile != null && (externalFile.getPath().contains("sdcard1") || externalFile.getPath().contains("sdcard0") || externalFile.getAbsolutePath().contains("/storage/emulated/0")))
                ret = true;
        }

        return ret;
    }

    public Properties getProperties() {
        FileInputStream ios = null;
        Properties pro = new Properties();

        try {
            ios = new FileInputStream(new File(mConfigPath));
            pro.load(ios);
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                if (ios != null)
                    ios.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return pro;
    }

    public String getConfig(String key) {
        Properties pos = getProperties();

        return (pos == null) ? null : pos.getProperty(key);
    }

    public int getConfigInt(String key) {
        int ret = 0;

        String value = getConfig(key);

        if (!isEmpty(value)) {
            ret = Integer.valueOf(value);
        }

        return ret;
    }

    public float getConfigFloat(String key) {
        float ret = 0;

        String value = getConfig(key);

        if (!isEmpty(value)) {
            ret = Float.valueOf(value);
        }

        return ret;
    }

    public long getConfigLong(String key) {
        long ret = 0;

        String value = getConfig(key);

        if (!isEmpty(value)) {
            ret = Long.valueOf(value);
        }

        return ret;
    }

    public boolean getConfigBoolean(String key) {
        boolean ret = false;

        String value = getConfig(key);

        if (!isEmpty(value)) {
            ret = Boolean.valueOf(value);
        }

        return ret;
    }

    public void setConfig(Properties properties) {
        Properties pos = getProperties();
        pos.putAll(properties);
        setProperties(pos);
    }

    public void setConfigBoolean(String key, boolean value) {
        setConfig(key, String.valueOf(value));
    }

    public void setConfigInt(String key, int value) {
        setConfig(key, String.valueOf(value));
    }

    public void setConfigLong(String key, Long value) {
        setConfig(key, String.valueOf(value));
    }

    public void setConfig(String key, String value) {
        if (value == null)
            return;
        Properties pos = getProperties();
        pos.put(key, value);
        setProperties(pos);
    }

    public void removeConfig(String... key) {
        Properties pos = getProperties();
        for (String str : key) {
            pos.remove(str);
        }
        setProperties(pos);
    }

    private void initProperties() {

    }

    private void setProperties(Properties properties) {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(new File(mConfigPath));
            properties.store(fos, "utf-8");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input) || "null".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }
}
